#ifndef _TTHREADPOOL_H_
#define _TTHREADPOOL_H_
#include <deque>
#include <mutex>
#include <vector>
#include <thread>
#include <algorithm>
#include <functional>
#include <condition_variable>
#include "tnocopyable.h"

namespace tlib
{
	typedef std::function<void()> TTask;
	class TThreadPool : TNoCopyable
	{
	public:
		TThreadPool();
		virtual ~TThreadPool();

		void Start(int numThreads);
		void Stop();
		void Run(const TTask& tk);
		void Run(TTask&& tk);
		TTask Take();
	private:
		bool running_;
		std::mutex mutex_;
		std::deque<TTask> queue_;
		std::condition_variable notempty_;
		std::vector<std::thread> threads_;

		void runInThread();
	};
}

#endif